package cn.t222.ch04.ex;

import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//@ControllerAdvice
//@RestControllerAdvice
public class CustomExceptionHandler {
    @ExceptionHandler(value = {Exception.class})
    public Object handler1(Exception e, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> error = new HashMap<>();
        error.put("msg", e.getMessage());
        error.put("date", new Date());
        error.put("code",response.getStatus());
        return error;
    }
    /*@ExceptionHandler(value = {MaxUploadSizeExceededException.class})
    @ResponseBody
    public Object handler1(MaxUploadSizeExceededException e, HttpServletResponse response) throws Exception {
        *//*response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("<h1>总文件太大了</h1>");
        response.getWriter().close();*//*
        Map<String, Object> error = new HashMap<>();
        error.put("msg", e.getMessage());
        error.put("code",500);
        return error;
    }
    @ExceptionHandler(value = {Exception.class})
    public void handler2(Exception e, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("<h1>出错了</h1>");
        response.getWriter().close();
    }*/
}
